<template>
	<!-- 添加小区 -->
	<view class="building-add">
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">残疾证图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="disabilityCertificateImg==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="disabilityCertificateImg"></image>
			</view>
		</view>
		<view class="add-content">
			<navigator class="add-item" url="../../Public/People" hover-class="none">
				<view class="item-left">残疾人员<text>*</text></view>
				<view class="item-right">{{ userId_text }}</view>
			</navigator>
			<view class="add-item">
				<view class="item-left">残疾证号码<text>*</text></view>
				<view class="item-right"><input type="number" v-model="disabilityCertificateCode" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入残疾证号码" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">残障等级<text>*</text></view>
				<view class="item-right" @click="gradeShow=true">{{ disabilityGrade_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">残障类型<text>*</text></view>
				<view class="item-right" @click="typeShow=true">{{ disabilityType_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">备注</view>
				<view class="item-right"><textarea v-model="remark" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入备注"></textarea></view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 残障等级 -->
		<u-select v-model="gradeShow" :list="gradeList" @confirm="confirmGradeShow"></u-select>
		<!-- 残障类型 -->
		<u-select v-model="typeShow" :list="typeList" @confirm="confirmtypeShow"></u-select>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				gradeShow:false,
				typeShow:false,
				gradeList:[],
				typeList:[],
				disabilityGrade:'',
				disabilityGrade_text:'请选择',
				disabilityType:'',
				disabilityType_text:'请选择',
				disabilityCertificateImg:'',
				disabilityCertificateCode:'',
				remark:'',
				userId:'',
				userId_text:'请选择',
				disabilityId:''
			}
		},
		onLoad(options) {
			this.disabilityId = options.id
		},
		mounted() {
			this.getDictionaries()
			this.getDictionaries2()
			this.getDisabilityById()
		},
		onShow() {
			if(!uni.getStorageSync('people')) return
			let User = uni.getStorageSync('people')
			// console.log(User)
			this.userId_text = User.name
			this.userId = User.id
		},
		methods:{
			getDisabilityById() {
				let params = {
					disabilityId:this.disabilityId
				}
				this.$u.api.getDisabilityById(params)
					.then(res => {
						console.log(res)
						if(res.code == 200 ) {
							this.disabilityCertificateCode = res.result.disabilityCertificateCode
							this.disabilityGrade = res.result.disabilityGrade
							this.gradeList.forEach(item => {
								if(item.value == res.result.disabilityGrade) {
									this.disabilityGrade_text = item.label
								}
							})
							this.disabilityType = res.result.disabilityType
							this.typeList.forEach(item => {
								if(item.value == res.result.disabilityGrade) {
									this.disabilityType_text = item.label
								}
							})
							this.disabilityCertificateImg = res.result.disabilityCertificateImg
							this.remark = res.result.remark
							this.userId = res.result.userId
							this.userId_text = res.result.userName
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 残障等级
			getDictionaries() {
				let params = {
					dictCode:'disability_grade_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.gradeList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取残障等级失败'
						})
					})
			},
			confirmGradeShow(e) {
				this.disabilityGrade = e[0].value
				this.disabilityGrade_text = e[0].label
			},
			// 残障类型
			getDictionaries2() {
				let params = {
					dictCode:'disability_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.typeList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取残障类型失败'
						})
					})
			},
			confirmtypeShow(e) {
				this.disabilityType = e[0].value
				this.disabilityType_text = e[0].label
			},
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.disabilityCertificateImg = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 提交
			handleSubmit() {
				if(this.disabilityCertificateImg == '') {
					return this.$refs.uToast.show({
						title: '请上传残疾证图片'
					})
				}
				if(this.disabilityCertificateCode == '') {
					return this.$refs.uToast.show({
						title: '请输入残疾证号码'
					})
				}
				if(this.disabilityGrade == '') {
					return this.$refs.uToast.show({
						title: '请选择残障等级'
					})
				}
				if(this.disabilityType == '') {
					return this.$refs.uToast.show({
						title: '请选择残障类型'
					})
				}
				if(this.userId == '') {
					return this.$refs.uToast.show({
						title: '请选择残疾人员'
					})
				}
				let params = {
					disabilityCertificateCode:this.disabilityCertificateCode,
					disabilityCertificateImg:this.disabilityCertificateImg,
					disabilityGrade:this.disabilityGrade,
					disabilityType:this.disabilityType,
					remark:this.remark,
					userId:this.userId,
					id:this.disabilityId
				}
				console.log(params)
				uni.removeStorageSync('people')
				this.$u.api.editDisability(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'修改成功',
								icon:'success'
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: err.message
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
